import axios from "axios"
import store from "@/store"
import { isShowToast } from "@/components/elementToast";

const instance = axios.create({
    // baseURL: 'http://192.168.113.249:8081/cms'
    // baseURL: 'http://kumanxuan1.f3322.net:8881/cms'
    baseURL: '/api'

});

// 添加请求拦截器
instance.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    // console.log("请求拦截前");

    let token = localStorage.getItem("x-auth-token")
    if (token) {
        config.headers['x-auth-token'] = token;
    }
    return config;
}, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
});

// 添加响应拦截器
instance.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    // console.log("响应拦截器");
    // console.log("这里是响应拦截器", response);

    if (response.data.code != 0 && response.data.code != 400 && response.data.code != 407) {
        // store.dispatch("showToast/asyncChangeIsShowToastModal", {
        //     flag: true,
        //      toastMsg: "统一提示：" + response.data.message,
        //     toastType: "error"
        // })
        isShowToast("统一提示" + response.data.message, "warning");
        return;
        //return undefined
    }

    return response.data;
}, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
});

export default instance;
